package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 户口迁出功能实现类
 */
@Service
public class HouseholdOutServiceImpl extends ServiceImpl<HouseholdOutMapper, HouseholdOut> implements HouseholdOutService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的HouseholdOut表mapper对象
     */
    @Autowired
    private HouseholdOutMapper _HouseholdOutMpper;

 			
    @Autowired
    private HouseholdMapper _HouseholdMapper;                        
			
 			
			
 			
    @Autowired
    private CensusInfoPersonMapper _CensusInfoPersonMapper;                        
			

    /**
     * 户口迁出分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<HouseholdOutDto> List(HouseholdOutPagedInput input) {

      
        //声明一个支持户口迁出查询的(拉姆达)表达式
        LambdaQueryWrapper<HouseholdOut> queryWrapper = Wrappers.<HouseholdOut>lambdaQuery()
                .eq(input.getId()!=null,HouseholdOut::getId,input.getId())
                .eq(input.getCreatorId()!=null,HouseholdOut::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入HouseholdId不为空,则进行精确查询
        if(input.getHouseholdId()!=null) {
            queryWrapper=queryWrapper.eq(HouseholdOut::getHouseholdId,input.getHouseholdId());
       	 }
     	 //如果前端搜索传入AuditStatus不为空,则进行精确查询
        if(input.getAuditStatus()!=null) {
            queryWrapper=queryWrapper.eq(HouseholdOut::getAuditStatus,input.getAuditStatus());
       	 }
     	 //如果前端搜索传入Reason不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getReason())) {
            queryWrapper=queryWrapper.eq(HouseholdOut::getReason,input.getReason());
       	 }
     	 //如果前端搜索传入AuditUserId不为空,则进行精确查询
        if(input.getAuditUserId()!=null) {
            queryWrapper=queryWrapper.eq(HouseholdOut::getAuditUserId,input.getAuditUserId());
       	 }
     	 //如果前端搜索传入AuditReason不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getAuditReason())) {
            queryWrapper=queryWrapper.eq(HouseholdOut::getAuditReason,input.getAuditReason());
       	 }
     	 //如果前端搜索传入CensusInfoPersonId不为空,则进行精确查询
        if(input.getCensusInfoPersonId()!=null) {
            queryWrapper=queryWrapper.eq(HouseholdOut::getCensusInfoPersonId,input.getCensusInfoPersonId());
       	 }
     	 //如果前端搜索传入BeforeAddress不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getBeforeAddress())) {
            queryWrapper=queryWrapper.eq(HouseholdOut::getBeforeAddress,input.getBeforeAddress());
       	 }
     	 //如果前端搜索传入NowAddress不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getNowAddress())) {
            queryWrapper=queryWrapper.eq(HouseholdOut::getNowAddress,input.getNowAddress());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(HouseholdOut::getCreationTime);
        //构建一个分页查询的model
        Page<HouseholdOut> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取户口迁出数据
        IPage<HouseholdOut> pageRecords= _HouseholdOutMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _HouseholdOutMpper.selectCount(queryWrapper);

        //把HouseholdOut实体转换成HouseholdOut传输模型
        List<HouseholdOutDto> items= Extension.copyBeanList(pageRecords.getRecords(),HouseholdOutDto.class);
        for (HouseholdOutDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的Household表信息
           HouseholdDto HouseholdDTO = new HouseholdDto();        
            Household  HouseholdEntity= _HouseholdMapper.selectOne(Wrappers.<Household>lambdaQuery().eq(Household::getId,item.getHouseholdId()));
           if(HouseholdEntity!=null) {  
          BeanUtils.copyProperties(HouseholdDTO,HouseholdEntity);
            item.setHouseholdDto(HouseholdDTO); 
           }
          
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto AuditUserDTO = new AppUserDto();        
            AppUser  AuditUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getAuditUserId()));
           if(AuditUserEntity!=null) {  
          BeanUtils.copyProperties(AuditUserDTO,AuditUserEntity);
            item.setAuditUserDto(AuditUserDTO); 
           }
          
           
          	            
           //查询出关联的CensusInfoPerson表信息
           CensusInfoPersonDto CensusInfoPersonDTO = new CensusInfoPersonDto();        
            CensusInfoPerson  CensusInfoPersonEntity= _CensusInfoPersonMapper.selectOne(Wrappers.<CensusInfoPerson>lambdaQuery().eq(CensusInfoPerson::getId,item.getCensusInfoPersonId()));
           if(CensusInfoPersonEntity!=null) {  
          BeanUtils.copyProperties(CensusInfoPersonDTO,CensusInfoPersonEntity);
            item.setCensusInfoPersonDto(CensusInfoPersonDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个户口迁出查询
     */
    @SneakyThrows
    @Override
    public HouseholdOutDto Get(HouseholdOutPagedInput input) {
       if(input.getId()==null)
        {
            return new HouseholdOutDto();
        }
        PagedResult<HouseholdOutDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new HouseholdOutDto();
    }

    /**
     *户口迁出创建或者修改
     */
    @SneakyThrows
    @Override
    public HouseholdOutDto CreateOrEdit(HouseholdOutDto input) {

        //声明一个户口迁出实体
        HouseholdOut HouseholdOut = new HouseholdOut();

        //把前端传入的input参数拷贝到户口迁出实体
        BeanUtils.copyProperties(HouseholdOut,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(HouseholdOut);

        //定义一个返回给前端的户口迁出传输模型
        HouseholdOutDto HouseholdOutDto=new HouseholdOutDto();

        //同理把操作的户口迁出实体拷贝给户口迁出传输模型
        BeanUtils.copyProperties(HouseholdOutDto,HouseholdOut);

        //把传输模型返回给前端
        return HouseholdOutDto;
    }



}
